﻿package com.hirokimo.display
{
	import com.hirokimo.event.LibLoaderEvent;
	
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	
    /**  
    * 当Lib载入完毕 
    * @eventType com.hirokimo.event.LibLoaderEvent.LIB_INIT  
    */  
    [Event(name="lib_init", type="com.hirokimo.event.LibLoaderEvent")]	
    /**  
    * 当Lib载入失败 
    * @eventType com.hirokimo.event.LibLoaderEvent.LIB_ERROR  
    */  
    [Event(name="lib_error", type="com.hirokimo.event.LibLoaderEvent")]	

	/**
	 * LibLoader 加载外部lib库 加载成功后可以通过 attachElement 和 getElement 获取UI.fla舞台上的元素和库内的类实例
	 * 
	 * @author	hirokimo
	 * 
	 * @example
	 * 
	 * 			<listing version="3.0">
 	 *				lib = new LibLoader();
	 *				lib.addEventListener(LibLoaderEvent.LIB_INIT,__onLoadComplete);
	 *				lib.load("ui.swf"); 
	 * 			
	 * 			function __onLoadComplete(evt:LibLoaderEvent):void
	 * 			{
	 * 				// 获取库内的类实例来add
	 *					var sp_instance:Sprite = lib.attachElement("Rison");
	 *					addChild(sp_instance);
	 *
	 *					// 获取舞台上的元素来add
	 *					var sp_stage:Sprite = lib.getElement("pic");
	 *					addChild(sp_stage);
	 * 			}
	 * 			</listing>
	 */	
	 	
	public class LibLoader extends EventDispatcher
	{
		private var loader:Loader;
		private var appDomain:ApplicationDomain;
		
		/**
		 * 构造函数
		 * 
		 */		
		public function LibLoader()
		{
			loader = new Loader();
			configureListeners(loader.contentLoaderInfo);	
		}		
		/**
		 * 加载Lib数据
		 * 
		 * @param _path Lib路径
		 */	
		public function load(_path:String):void
		{
			loader.load(new URLRequest(_path));	
		}
		/**
		 * 获取Lib库内的相关类实例
		 * 
		 * @_name 库内的类名
		 */	
		public function attachElement(_name:String):Sprite
		{
			var ClassReference:Class =  appDomain.getDefinition(_name) as Class;
			var tmp:Sprite = new ClassReference() as Sprite;
			return tmp;
		}
		/**
		 * 获取Lib舞台上的元素
		 * 
		 * @_name 元素的name
		 */	
		public function getElement(_name:String):*
		{
			var content:* = loader.content;
			var tmp:* = content.getChildByName(_name);
			return tmp;
		}
		private function configureListeners(dispatcher:LoaderInfo):void 
		{
			dispatcher.addEventListener(Event.COMPLETE, __onLoadComplete);
			dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, __onHttpStatus);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, __onIOError);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, __onProgress);
		}
		private function destroyListeners(dispatcher:LoaderInfo):void 
		{
			dispatcher.removeEventListener(Event.COMPLETE, __onLoadComplete);
			dispatcher.removeEventListener(HTTPStatusEvent.HTTP_STATUS, __onHttpStatus);
			dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, __onIOError);
			dispatcher.removeEventListener(ProgressEvent.PROGRESS, __onProgress);
		}
		private function __onLoadComplete(evt:Event):void 
		{
			var loaderInfo:LoaderInfo = evt.target as LoaderInfo;			
			createAppDomain(loaderInfo);
			destroyListeners(loaderInfo);
			var e:LibLoaderEvent = new LibLoaderEvent(LibLoaderEvent.LIB_INIT); 
			dispatchEvent(e);
		}
		private function __onHttpStatus(event:HTTPStatusEvent):void 
		{
			//trace("__onHttpStatus: " + event);
		}

		private function __onIOError(event:IOErrorEvent):void 
		{
			var e:LibLoaderEvent = new LibLoaderEvent(LibLoaderEvent.LIB_ERROR);
			e.args = {text:event.text};
			dispatchEvent(e);
		}
        
		private function __onProgress(event:ProgressEvent):void 
 		{
			//trace("__onProgress: 加载 =" + event.bytesLoaded + " 总共 =" + event.bytesTotal);
		}
		/**
		 * 创建被加载swf的应用程序域
		 * 
		 * @loadInfo 加载库的loadInfo
		 */	
		private function createAppDomain(loadInfo:LoaderInfo):void
		{			
			appDomain = loadInfo.applicationDomain as ApplicationDomain;
		}
	}
}